import org.junit.Test;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author MR.Z
 * @date 2020/5/7 9:23
 */
public class CommonTest2 {

    @Test
    //将list<map>根据字段的值转为map
    public   void test111() {
        // 假设你有一个 List<Map>，其中包含多个 Map，每个 Map 都有 "name" 和 "age" 作为键
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "zhangsan");
        map1.put("age", 24);
        list.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "lisi");
        map2.put("age", 30);
        list.add(map2);
        Map<String, Integer> resultMap = list.stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("name"), // 使用姓名作为键
                        map -> (Integer) map.get("age")  // 使用年龄作为值
                ));

        // 输出结果
        System.out.println(resultMap); // 输出：{zhangsan=24, lisi=30}
    }


    @Test
    public void test01() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        SimpleDateFormat sdfF = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.HOUR_OF_DAY, -1);
        String t = sdf.format(c.getTime());
        System.out.println(t);

    }

    @Test
    public void test02() {

        String a = "123.00,333.33";
        double[] doubleArray = Arrays.stream(a.split(","))
                .mapToDouble(Double::parseDouble)
                .toArray();

        // 输出结果
        Arrays.stream(doubleArray).forEach(System.out::println);
        System.out.println(doubleArray);

    }

    @Test
    public void test03() {
        String a = "sdfsdfs\raaa";
        String b = "sdfsdfs\naaa";
        String c = "sdfsdfs\r\naaa";
        System.out.println(a);
        System.out.println("====");
        System.out.println(b);
        System.out.println("====");
        System.out.println(c);
    }

    @Test
    public void test04() {
        System.out.println(1 << 8);
        System.out.println(32 >>> 2);
        System.out.println(32 >> 2);
        String separator = File.separator;
        System.out.println(separator);
    }

    @Test
    public void test05() {


        Date nextRemindTime = new Date(); // 假设为某个具体的 Date 类型对象

        // 拆分日期和时间
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String reminderDay = dateFormat.format(nextRemindTime);
        String reminderTime = timeFormat.format(nextRemindTime);

        // 从时间戳获取时分秒
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nextRemindTime);
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date reminderTimeDate = calendar.getTime();

        // 输出结果
        System.out.println("reminderDay: " + reminderDay);
        System.out.println("reminderTime: " + reminderTime);
        System.out.println("reminderTimeDate: " + reminderTimeDate);

    }

    @Test
    public void test06() {

        getDateList("日", "202001", "202003");

    }

    private void getDateList(String timeType, String startTime, String endTime) {
        SimpleDateFormat sdfD = null;
        if (timeType.equals("小时")) {
            sdfD = new SimpleDateFormat("yyyy-MM-dd HH");
        }
        Calendar c = Calendar.getInstance();
        List<String> xAils = new ArrayList<>();
        try {
            Date sd = sdfD.parse(startTime);
            Date ed = sdfD.parse(endTime);
            c.setTime(sd);
            while (c.getTime().before(ed) || c.getTime().equals(ed)) {
                String format = sdfD.format(c.getTime());

                if (timeType.equals("yyyyMMddHH") || timeType.equals("yyyy-MM-dd HH")) {
                    c.add(Calendar.HOUR_OF_DAY, 1);
                } else if (timeType.equals("yyyyMMdd") || timeType.equals("yyyy-MM-dd")) {
                    c.add(Calendar.DAY_OF_MONTH, 1);
                } else if (timeType.equals("yyyyMM") || timeType.equals("yyyy-MM")) {
                    c.add(Calendar.MONTH, 1);
                } else if (timeType.equals("yyyy")) {
                    c.add(Calendar.MONTH, 1);
                } else {
                    throw new RuntimeException("暂不支持的时间格式");
                }
                xAils.add(format);
            }
        } catch (ParseException e) {
            throw new RuntimeException("日期格式解析错误");
        }
        System.out.println(xAils);
    }

    @Test
    public void test07() {
        System.out.println(16 * 22.5);
        List<Double> list = new ArrayList<>();
        for (int i = 0; i < 16; i++) {
            list.add(i * 22.5);
        }
        System.out.println(list);
    }

    @Test
    public void test08() {
        Object o = new Object();
        List list = new ArrayList();
        list.add("1");
        List list1 = new ArrayList();
        list1.add("1");
        o = list1;
        boolean equals = o.equals(list);
        System.out.println(equals);
    }

    @Test
    public void test09() {

    }

    @Test
    public void test10() {
        Object a = null;
        String s = (String) (a);
        System.out.println(s);
//        Double aDouble = Double.valueOf(s);

        Object b = 1.2;
        Object c = 0.5;
        double v = (Double) b / (Double) c;
        System.out.println(v);


    }

    @Test
    public void test11() {
        Date date = new Date();
        String s = date.toString();
        System.out.println(date);
        System.out.println(s);
    }

    @Test
    public void test12() {
        int i = 1;
        i = i++;  //i-> 1
        int j = i++; //i->2  j=1
        int k = i + ++i * i++;//2+3*3
        System.out.println(i);//4
        System.out.println(j);//1
        System.out.println(k);//8

    }

    @Test
    public void test13() {
        int i = 1;
        int j = i + ++i * i++;//1+2*2
        System.out.println(i);
        System.out.println(j);

    }

    @Test
    public void test14() {
        int a = 9;
        int b = 8;
        int c = 8;
        int d = 9;
        System.out.println(a % b);
        System.out.println(a & b);

        System.out.println(c % d);
        System.out.println(c & d);
    }


    public static int dayOfYearByDateFormat(int year, int month, int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("DDD");
        return Integer.parseInt(sdf.format(new Date(year, month - 1, day)));
    }

    public static int dayOfYearByCalendar(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    public static int dayOfYearByLocalDate(int year, int month, int day) {
        LocalDate date = LocalDate.of(year, month, day);
        return date.getDayOfYear();
    }

    @Test
    public void test15() {
        int i = dayOfYearByDateFormat(2020, 9, 24);
        int i1 = dayOfYearByCalendar(2020, 9, 24);
        int i2 = dayOfYearByLocalDate(2020, 9, 24);
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
    }

    @Test
    public void test16() {

        String reminderDay = "2023-07-18";
        Date reminderTime = new Date(); // 假设提醒时间为当前时间（示例）

        LocalDate localDate = LocalDate.parse(reminderDay);
        LocalTime localTime = LocalDateTime.ofInstant(reminderTime.toInstant(), ZoneId.systemDefault()).toLocalTime();

        LocalDateTime combinedDateTime = LocalDateTime.of(localDate, localTime);

        System.out.println(combinedDateTime);
        LocalDateTime currentDate = LocalDateTime.now();
        System.out.println(currentDate);
        long minute = combinedDateTime.until(currentDate, ChronoUnit.SECONDS) / 60;
//        long minute = Math.abs(combinedDateTime.until(currentDate, ChronoUnit.SECONDS)) / 60;
        System.out.println(minute);


    }

    private static Date convertToDate(LocalDate localDate, int year) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(year + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        //会导致ConcurrentModificationException
//        List<String> list = new ArrayList<>();
//
//        for (int i = 0; i < 30; i++) {
//            new Thread(() -> {
//                list.add(UUID.randomUUID().toString().substring(0, 8));
//                System.out.println(Thread.currentThread().getName()+"\t"+list);
//            }, String.valueOf(i)).start();
//        }
        //解决方案1 vector
//        List<String> list1 = new Vector<>();
//
//        for (int i = 0; i < 30; i++) {
//            new Thread(() -> {
//                list1.add(UUID.randomUUID().toString().substring(0, 8));
//                System.out.println(Thread.currentThread().getName()+"\t"+list1);
//            }, String.valueOf(i)).start();
//        }
        //解决方案2 Collections.synchronizedList(new ArrayList<>())
        List<String> list2 = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                list2.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(Thread.currentThread().getName() + "\t" + list2);
            }, String.valueOf(i)).start();
        }
    }

    @Test
    public void test17() {
        Integer a = 100;
        Integer b = 100;
        System.out.println(a==b);
        Integer c = 1001;
        Integer d = 1001;
        System.out.println(c==d);

        int e = 100;
        int f = 100;
        System.out.println(e==f);
        int q = 1001;
        int w = 1001;
        System.out.println(q==w);
        //true
        //false
        //true
        //true
    }

}
